/***********
 **	框架页js，下面的所有方法均为单例模式，一个页面只能实现一个实例！！！
 ********** */
var homePageUrl=null;
var homePageName = "首页"
/**系统时间**/
FrameTime={
		setSysTime:function(id){
			var time=mini.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
			$("#"+id).html(time);
		},
		//初始化系统时间，定时更新
		initial:function(id){
			var that=this;
			that.setSysTime(id);
			setInterval(function(){
				that.setSysTime(id);
			},1000);
		}
}


/**菜单（只支持2级菜单）**/
FrameMenu={
		isMenuHide:false,//当前菜单隐藏或显示状态，默认显示
		//初始化菜单
		initial:function(id,ip,imgFolder){
			//从后台获取菜单数据
			var that=this;
			$.ajax({
				url:ctx+"/sys/getMenu.do",
				success:function(data){
					if(data&&data.length>0){
						//创建菜单
						that.createMenu(id,data,imgFolder);
						//加载一级菜单hover事件
						that.menuLv1event();
						//加载一级菜单点击事件
						that.menuLv1click();
						//加载二级菜单点击事件
						that.menuLv2click();
					}else{
						showAlert("对不起，</br>您的权限不足！","warn","警告",function(){
							location.href=ctx+'/loginOut.do?ip='+ip;
				    	});
					}
				}
			});
		},
		//生成菜单dom
		createMenu:function(id,data,imgFolder){
			var menu_html="";
			if(data){
				for(var i=0;i<data.length;i++){
					var menuItem=data[i];//一级菜单			
					var title=menuItem.text;
					var url=menuItem.info.url;
					var children=menuItem.children;//二级菜单
					if(title==homePageName&&url!=null&&url!=""){
						homePageUrl=url;
					}
					menu_html+="<li>";
					menu_html+=	"	<div class='frame-menu-Lv1' isChecked='false' isHover='false' name='"+title+"' url='"+url+"'>";
                    //存在菜单图标时加载图标
					 //存在菜单图标时加载图标
					var iconCls=[""];
					if(menuItem.info.iconCls!=undefined&&menuItem.info.iconCls!=null){//旧版权限系统返回iconCls
						iconCls=menuItem.info.iconCls.split(",");
					}else if(menuItem.info.iconcls!=undefined&&menuItem.info.iconcls!=null){//新版权限系统返回iconcls
						iconCls=menuItem.info.iconcls.split(",");
					}
                	var menu_img_=iconCls[0];
                    menu_html+="<div  class='frame-menu-Lv1-icon'><img src="+imgFolder+"/"+menu_img_+" alt=''/></div>"
					menu_html+=		"<span>"+title+"</span>";
					menu_html+=	"	</div>";
					if(children&&children.length>0){
						menu_html+=	"<ul style='display: none' class='frame-menu-Lv2'>";
						for(var j=0;j<children.length;j++){
							var menuItem2=children[j];//二级菜单
							var title2=menuItem2.text;
							var url2=menuItem2.info.url;
							if(title2==homePageName&&homePageUrl!=null&&url!=null&&url!=""){
								homePageUrl=url2;
							}
							menu_html+=	"<li class='frame-menu-Lv2-item' name='"+title2+"' url='"+url2+"'>";
							menu_html+=		title2;
							menu_html+=	"</li>";
						}
						menu_html+=	"</ul>";
					}
					menu_html+="</li>";
				}
			}
			$("#"+id).html(menu_html);
		},
		//一级菜单hover事件
		menuLv1event:function(){
			var that=this;
			$(".frame-menu-Lv1").parent().hover(function(){	
				that.checkedMenu(this,"isHover");
			},function(){
				that.cancelCheckedMenu(this,"isHover");
			});
		},
		//一级菜单点击事件（有url地址直接打开）
		menuLv1click:function(){
			var that=this;
			$(".frame-menu-Lv1").click(function(){
				that.checkedMenu($(this).parent(),"isChecked");
			});
		},
		//一级菜单菜单点击撑开选中事件
		checkedMenu:function(e,type){
			var menuLv1 = $(e).find(".frame-menu-Lv1");
			var menuLv2 = $(e).find("ul");
			//切换一级菜单为选中状态；
			menuLv1.attr(type,'true');
			menuLv1.addClass("frame-menu-Lv1-active");
			//显示二级菜单
			if(menuLv2!=null&&menuLv2.length!=0){
				menuLv2.show();
			}else{
				//点击事件时没有二级菜单时直接打开页面
				if(type=="isChecked"){
					FrameTab.addTab(menuLv1.attr("name"),menuLv1.attr("name"),menuLv1.attr("url"));
				}
			}
		},
		//一级菜单点击收起取消事件
		cancelCheckedMenu:function(e,type){
			var menuLv1 = $(e).find(".frame-menu-Lv1");
			var menuLv2 = $(e).find("ul");
			//切换一级菜单图标为取消选中状态
			menuLv1.attr(type,'false');
			menuLv1.removeClass("frame-menu-Lv1-active");
			//隐藏二级菜单
			menuLv2.hide();
		},
		//二级菜单点击事件
		menuLv2click:function(){
			var that=this;
			$(".frame-menu-Lv2-item").click(function(){
				//加载tab页
				if($(this).attr("name")=="首页"){
					FrameTab.addTab($(this).attr("name"),"首页",$(this).attr("url"));
				}else{
					FrameTab.addTab($(this).attr("name"),$(this).parent().prev().attr("name"),$(this).attr("url"));
				}
				//隐藏二级菜单
				that.cancelCheckedMenu($(this).parent().parent()[0],"isHover");
			});
		},
} 

/**tab页签**/
FrameTab={
	tabs:null,
	currentTab:null,
	//初始化
	initial:function(id){
		this.tabs=mini.get(id);
	},
	//tab右键菜单onBeforeOpen
	onBeforeOpen:function(e){
		FrameTab.currentTab=FrameTab.tabs.getTabByEvent(e.htmlEvent);
		if(!FrameTab.currentTab){
			e.cancel=true;
		}
	},
	//tab右键菜单-关闭其他
	closeAllBut:function(){
		var but=[FrameTab.currentTab];
		but.push(FrameTab.tabs.getTab("首页"));
		FrameTab.tabs.removeAll(but);
	},
	//tab右键菜单-关闭所有
	closeAll:function(){
		var but=[];
		but.push(FrameTab.tabs.getTab(homePageName));
		FrameTab.tabs.removeAll(but);
	},
	//tab右键菜单-关闭当前
	closeTab:function(){
		if(FrameTab.currentTab.name==homePageName){
			showFailTip(homePageName+"无法关闭");
		}else{
			FrameTab.tabs.removeTab(FrameTab.currentTab);
		}
	},
	//增加tab页
	addTab:function(title,name,url){
		if(url==""||url==null) {
			return;
		}
		if(name!="首页"){
			name=title+name;
		}
		var tab=this.tabs.getTab(name);
		if(tab==undefined){
			tab={
					title:title,
					name:name
			}
			//这里拼接了url，实际项目，应该从后台直接获得完整的url地址
			if(url.indexOf("http://")!=-1||url.indexOf("https://")!=-1){
				tab.url=url;
			}else if(url.indexOf("\$\{")!=-1&&url.indexOf("\}")!=-1){
				var key=url.substring(url.indexOf("\$\{")+2,url.indexOf("\}"));
				var val=getProperties(key);
				tab.url=url.replace("\$\{"+key+"\}",val);
			}else{
				tab.url=ctx+url;
			}
			if(name==homePageName){
				tab.showCloseButton=false;
			}else{
				tab.showCloseButton=true;
			}
			this.tabs.addTab(tab);
			this.tabs.activeTab(tab);
		}else{
			this.tabs.activeTab(tab);
			//不重载页面，防止被保存数据丢失?
			this.tabs.reloadTab(tab);
		}
	},
	//首页显示
	showHomePage:function(title,url){
		this.addTab(title,"首页",url);
	},
	doLayout:function(){
		this.tabs.doLayout();
	}
	
}

function getProperties(key){
	var result;
	$.ajax({
		url:ctx+"/sys/getProperties.do",
		data:{
			key:key
		},
		async:false,
		success:function(data){
			result=data;
		}
	});
	return result;
}

/**全屏**/
FullScreen={
	open:function(){
		var docElm=document.documentElement;
		if(docElm.requestFullscreen){//W3C
			docElm.requestFullscreen();
		}else if(docElm.mozRequestFullScreen){//FireFox
			docElm.mozRequestFullScreen();
		}else if(docElm.webkitRequestFullScreen){//Chrome等
			docElm.webkitRequestFullScreen();
		}else if(docElm.msRequestFullscreen){//IE11
			docElm.msRequestFullscreen();
		}
		$("#fullScreen_open").hide();
		$("#fullScreen_closed").show();
	},
	closed:function(){
		if(document.exitFullscreen){
			document.exitFullscreen();
		}else if(document.mozCancelFullScreen){
			document.mozCancelFullScreen();
		}else if(document.webkitCancelFullScreen){
			document.webkitCancelFullScreen();
		}else if(document.msExitFullscreen){
			document.msExitFullscreen();
		}
		$("#fullScreen_open").show();
		$("#fullScreen_closed").hide();
	}
}